static mut module_instance: *mut ArcModule = 0 as *mut ArcModule;

mod color;
mod arc_module;
mod utils;

use color::Rgb;
use arc_module::ArcModule;

pub use utils::init;
pub use utils::getAnimationBuffer;


struct Glow {
  pub color: Rgb,
  pub duration: u8
}

const hello_world : [u64; 44] = [
 0b000000000000000000000000000000000000,
 0b000000000000000000000000000000000000,
 0b000000000000000000000000000001111000,
 0b011111111000000000000000000010000100,
 0b000001000000000000000000000100000010,
 0b000001000000000000000000000100000010,
 0b000001000000000000000000000100000010,
 0b000001000000000000000000000100000010,
 0b011111111000000000000000000111111110,
 0b000000000000000000000000000000000000,
 0b000000000000000000000000000000000010,
 0b011111111000000000000000000000000010,
 0b010001001000000000000000000000000010,
 0b010001001000000000000000000000000010,
 0b010001001000000000000000000000000010,
 0b010000001000000000000000000111111110,
 0b010000001000000000000000000000000000,
 0b000000000000000000000000000000000000,
 0b000000000000000000000000000011000110,
 0b011111111000000000000000000100101000,
 0b010000000000000000000000000100110000,
 0b010000000000000000000000000100100000,
 0b010000000000000000000000000100100000,
 0b010000000000000000000000000111111110,
 0b010000000000000000000000000000000000,
 0b000000000000000000000000000001111000,
 0b000000000000000000000000000010000100,
 0b011111111000000000000000000100000010,
 0b010000000000000000000000000100000010,
 0b010000000000000000000000000100000010,
 0b010000000000000000000000000010000100,
 0b010000000000000000000000000001111000,
 0b010000000000000000000000000000000000,
 0b000000000000000000000000000111100000,
 0b000111100000000000000000000000011100,
 0b001000010000000000000000000000000010,
 0b010000001000000000000000000000000100,
 0b010000001000000000000000000000001000,
 0b010000001000000000000000000000000100,
 0b001000010000000000000000000000000010,
 0b000111100000000000000000000000011100,
 0b000000000000000000000000000111100000,
 0b000000000000000000000000000000000000,
 0b000000000000000000000000000000000000,
];

impl Glow {
  pub fn fill_frame(&self, frame: &mut[Rgb], rows: usize, cols: usize, offset: u8) {
    let mut intensity: f32 = offset as f32 / (self.duration as f32 / 2f32);
    // intensity = 1;
    if intensity > 1f32 {
      intensity = 2f32 - intensity;
    };
    
    let color = Rgb {
      r: (self.color.r as f32 * intensity) as u8,
      g: self.color.g * intensity as u8,
      b: self.color.b * intensity as u8,
    };

    for row in 0 .. rows {
      let text_pixels = hello_world[row];
      for col in 0 .. cols {
        if text_pixels >> (cols-col - 1 as usize) & 1 == 1 { continue; }
        frame[row * cols + col] = color;
      }
    }
  }
}

#[no_mangle]
pub extern fn apply() {
  let mut module = ArcModule::get_instance();
  let rows = module.rows;
  let cols = module.cols;
  let ref mut animation = module.get_animation().as_mut_slice();
  let glow = Glow {
    color: Rgb {r: 0xff, g: 0, b: 0x2},
    duration: 70
  };

  for (index, frame) in animation.chunks_mut(rows * cols).enumerate() {
    glow.fill_frame(frame, rows, cols, (index % glow.duration as usize) as u8);
  }
}
